package com.et.machine.store

import androidx.annotation.IntDef
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.Ignore
import androidx.room.PrimaryKey

/**
 * Table of out goods
 */
@Entity(tableName = "table_out_goods_order")
data class OutGoodsRecordEntity(
    @PrimaryKey @ColumnInfo(name = "id") var uid: Long = 0L,
    @ColumnInfo(name = "flag") var flag: String = "",
)


@Retention(value = AnnotationRetention.SOURCE)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY, AnnotationTarget.VALUE_PARAMETER)
@IntDef(UNDEFINE, ENABLED, DISABLED, LACK)
annotation class Status

const val UNDEFINE: Int = 0
const val ENABLED: Int = 1
const val DISABLED: Int = 2
const val LACK: Int = 3

/**
 * Table of goods
 */
@Entity(tableName = "table_channel")
data class ChannelEntity(
    @PrimaryKey @ColumnInfo(name = "id") var uid: Int = 0,
    @ColumnInfo(name = "box") var box: Int = 0,
    @ColumnInfo(name = "channel") var channel: Int = 0,
    @ColumnInfo(name = "label") var label: String = "",
    @ColumnInfo(name = "price") var priceCent: Int = 0,
    @ColumnInfo(name = "status") var status: Int = UNDEFINE,
    @ColumnInfo(name = "quantity") var quantity: Int = 0,
    @ColumnInfo(name = "barcode") var barcode: String = "${box}0$channel",
    @ColumnInfo(name = "name") var name: String = "UNDEFINE",
    @ColumnInfo(name = "image_path_local") var image: String = "",
    @ColumnInfo(name = "discount") var discount: Int = 100,
    @Ignore var sid: Int = 0
) {

//    @Status
//    fun getState(): Int {
//        return when {
//            status == 0 -> UNDEFINE
//            getCount() == 0 -> {
//                LACK
//            }
//            getGoodsId() == 0xEE -> {
//                DISABLED
//            }
//            else -> ENABLED
//        }
//    }
//
//    fun getGoodsId(): Int {
//        return status shl 24 shr 24
//    }
//
//    fun setGoodsId(goodsId: Int) {
//        if (goodsId !in 0..256) return
//        status = status shr 8 shl 8 or goodsId
//    }
//
//
//    fun setGoodsCount(number: Int) {
//        if (number !in 0..20) return
//        status = (status and 0xff) or (number shl 8)
//    }
//
//
//    fun getCount(): Int {
//        return when (box) {
//            6, 7, 8, 9 -> status shr 8 and 0xff
//            else -> 1
//        }
//    }

    companion object {
        private fun uid(box: Int, channel: Int): Int = box * 1000 + channel
        fun label(box: Int, channel: Int): String {
            if (box == 11) return String.format("%02d", channel)
            if (box < 5) return String.format("%d%02d", box, channel)
            if (box < 10) return String.format("")
            return "$box$channel"
        }

        fun create(box: Int, channel: Int): ChannelEntity {
            return ChannelEntity(
                uid = uid(box, channel),
                box = box,
                channel = channel,
                label = label(box, channel),
                priceCent = 0,
                discount = 100,
                status = UNDEFINE,
                quantity = 0
            )
        }

        fun create(
            box: Int,
            channel: Int,
            priceCent: Int,
            barcode: String,
            discount: Int = 100,
            @Status status: Int = UNDEFINE,
            quantity: Int = 0
        ): ChannelEntity {
            return ChannelEntity(
                uid = uid(box, channel),
                box = box,
                channel = channel,
                label = label(box, channel),
                priceCent = priceCent,
                barcode = barcode,
                discount = discount,
                status = status,
                quantity = quantity
            )
        }
    }

    override fun toString(): String {
        return "ChannelEntity(uid=$uid, box=$box, channel=$channel, label='$label', priceCent=$priceCent, status=$status, quantity=$quantity, barcode='$barcode', name='$name', image='$image', discount=$discount, sid=$sid)"
    }

}
